package com.ruoyi.safe.controller.web;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.safe.domain.BizProjectCheck;
import com.ruoyi.safe.domain.dto.EntityIdsDTO;
import com.ruoyi.safe.service.IBizProjectCheckService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * 项目自检Controller ----->张鹏
 *
 * @author ruoyi
 * @date 2023-11-17
 */
@RestController
@RequestMapping("/check/check")
public class BizProjectCheckController extends BaseController
{
    @Autowired
    IBizProjectCheckService bizProjectCheckService;

    /**
     * 查询项目自检列表
     */
    @PreAuthorize("@ss.hasPermi('check:check:list')")
    @GetMapping("/list")
    public TableDataInfo list(BizProjectCheck bizProjectCheck)
    {
        startPage();
        PageUtils.orderBy("create_time desc");
        List<BizProjectCheck> list = bizProjectCheckService.selectBizProjectCheckList(bizProjectCheck);
        return getDataTable(list);
    }

    /**
     * 导出项目自检列表
     */
    @PreAuthorize("@ss.hasPermi('check:check:export')")
    @Log(title = "项目自检", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BizProjectCheck bizProjectCheck)
    {
        List<BizProjectCheck> list = bizProjectCheckService.selectBizProjectCheckList(bizProjectCheck);
        ExcelUtil<BizProjectCheck> util = new ExcelUtil<BizProjectCheck>(BizProjectCheck.class);
        util.exportExcel(response, list, "项目自检数据");
    }

    /**
     * 获取项目自检详细信息
     */
    @PreAuthorize("@ss.hasPermi('check:check:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        BizProjectCheck bizProjectCheck = bizProjectCheckService.selectBizProjectCheckById(id);
        //是否有权限新增记录
        if(bizProjectCheck.getCheckPeople() != null){
            List<String> checkPeoples = Arrays.asList(bizProjectCheck.getCheckPeople().split(","));
            if(checkPeoples.contains(SecurityUtils.getUsername())){
                bizProjectCheck.setHasAddRecordPermission(true);
            }else{
                bizProjectCheck.setHasAddRecordPermission(false);
            }
        }
        return success(bizProjectCheck);
    }

    /**
     * 新增项目自检
     */
    @PreAuthorize("@ss.hasPermi('check:check:add')")
    @Log(title = "项目自检", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BizProjectCheck bizProjectCheck)
    {
        return toAjax(bizProjectCheckService.insertBizProjectCheck(bizProjectCheck));
    }

    /**
     * 修改项目自检
     */
    @PreAuthorize("@ss.hasPermi('check:check:edit')")
    @Log(title = "项目自检", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BizProjectCheck bizProjectCheck)
    {
        return toAjax(bizProjectCheckService.updateBizProjectCheck(bizProjectCheck));
    }
    /**
     * 删除项目自检
     */
    @PreAuthorize("@ss.hasPermi('check:check:remove')")
    @Log(title = "项目自检", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id)
    {
        return toAjax(bizProjectCheckService.deleteBizProjectCheckById(id));
    }
    /**
     * 批量删除项目自检
     */
    @PreAuthorize("@ss.hasPermi('check:check:remove')")
    @Log(title = "项目自检", businessType = BusinessType.DELETE)
    @DeleteMapping("/deleteByIds")
    public AjaxResult remove(@RequestBody @Validated EntityIdsDTO entityIdsDTO)
    {
        return toAjax(bizProjectCheckService.deleteBizProjectCheckByIds(entityIdsDTO.getIds()));
    }
}
